<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Validate;
class Adminhos extends Base1
{
    //预约列表
    public function index()
    {
        $hospital_id=\think\Session::get('hospital_id');
        //查预约清单
        $res1 = DB::name('appointment')
            ->alias('a')
            ->join('child c','a.child_id=c.child_id')
            ->join('vaccine v','a.vaccine_id=v.vaccine_id')
            ->where('hospital_id',$hospital_id)
            // ->field('child_name,vaccine_name,begin_time')
            ->where('appointment_status',0)
            ->order('appointment_id desc')
            ->select();
        //  dump($res1);
        // exit;
        $this->assign("res1",$res1);
         //查预约通过清单
        $res2 = DB::name('appointment')
            ->alias('a')
            ->join('child c','a.child_id=c.child_id')
            ->join('vaccine v','a.vaccine_id=v.vaccine_id')
            ->where('hospital_id',$hospital_id)
            ->where('appointment_status',1)
            ->order('appointment_id desc')
            ->select();
        $this->assign("res2",$res2);
       //查预约取消清单
    //    $res3 = DB::name('appointment')
    //         ->alias('a')
    //         ->join('child c','a.child_id=c.child_id')
    //         ->join('vaccine v','a.vaccine_id=v.vaccine_id')
    //         ->where('hospital_id',$hospital_id)
    //         ->where('appointment_status',2)
    //         ->order('appointment_id desc')
    //         ->select();
    //     $this->assign("res3",$res3);
        //查预约全部清单
        $res4 = DB::name('appointment')
            ->alias('a')
            ->join('child c','a.child_id=c.child_id')
            ->join('vaccine v','a.vaccine_id=v.vaccine_id')
            ->where('hospital_id',$hospital_id)
            ->where('appointment_status','<',2)
            ->order('appointment_id desc')
            ->select();
        $this->assign("res4",$res4);
        return $this->fetch();
    }
    //处理通过
    public function pass($appointment_id)
    {
        // dump($appointment_id);
        // exit;
        $data = [
            'appointment_status'=>1,
        ];
        $res = Db::name('appointment')->where('appointment_id',$appointment_id)->update($data);
        if($res)
        {
            return $this->success('通过成功','adminhos/index');
        }
        else{
            return  $this->error('失败，请重新操作');
        }
    }
    //修改密码
    public function updatepw()
    {
        return $this->fetch();
    }
     //处理修改密码
     public function updatepw1()
     {
        $hos_id = \think\Session::get('hos_id');
        $hos_pw = md5(input('pw'));
        $res = Db::name('hos')->where('hos_id',$hos_id)->where('hos_pw',$hos_pw)->find();
        if($res == null){
            return  $this->error('原密码错误，请重新输入');
        }else
        {
            $date = [
                'hos_pw' =>md5(input('pw1')),
             ];
            //  dump($date);
            //  exit;
             $res1 = Db::name('hos')->where('hos_id',$hos_id)->update($date);
             if($res1)
            {
                return $this->success('修改成功','adminhos/index');
            }
            else{
                return  $this->error('失败，请重新操作');
            }
        }
       
     }
    // 日期管理
    public function date()
    {
        $hospital_id =Session::get('hospital_id');
        $res = DB::name('a_date')
           
            ->where('hospital_id',$hospital_id)
            ->order('date desc')
            ->select();
        $this->assign("res",$res);
        return $this->fetch();
    }
    // 添加预约人数
    public function add()
    {
        $date = date('Y-m-d');
        $token = input("__token__");//获取提交过来的令牌
        $data = [

            '__token__' => $token,
            'date' => input("date"),
        ];
        
        $rule = [

            '__token__' => 'token',
            'date'   => "after:$date",
        ];
        // dump($rule);
        // exit;
        //错误信息
        $msg = [

            '__token__' => '请不要刷新该页面！',
            'date'   => '请选择今天之后的日期',
        ];

        //引入验证类
        $validate = Validate::make($rule,$msg);
        if (!$validate->check($data)) {
       //如果验证不通过则错误页面并显示错误信息，3s后跳转指定页面
            $this->error($validate->getError(), url("adminhos/date"));
        }else{ 
       //通过后则正常进行
       $hospital_id =Session::get('hospital_id');
        $data1 = [
            'count' => input('count'),
            'date' => input('date'),
            'hospital_id' => $hospital_id,
        ];
        $a = input('date');
        
        $ri = Db::name('a_date')->where('date',$a)->where('hospital_id',$hospital_id)->find();
        
        if(!($ri==null))
        {
            return  $this->error('此日期已经有添加过~');
        }
        $res = Db::name('a_date')->insert($data1);
        // dump($res);
        // exit;
        if($res)
        {
            return $this->success('添加成功','adminhos/date');
        }
        else{
            return  $this->error('失败，请重新填写信息');
        }
        }       
        
    }
    // 删除日期
    public function delete($id)
    {
        $res = Db::name('a_date')->delete($id);
        // dump($res);
        // exit;
        if($res)
        {
            return $this->success('删除成功','adminhos/date');
        }
        else{
            return  $this->error('失败，请重新填写信息');
        }
    }
}